package com.china08.yxyapi.api.home.activity.declaration;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.huodong.declaration.YxyHuodongHrbDeclaration;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.declaration.YxyHuodongHrbDeclarationRepository;

/**
 * 
 * 哈尔滨教师精品宣言
 *
 */
@CrossOrigin
@RestController
public class ActivityHrbDeclarationControler extends YxyApiBaseController {

    @Autowired
    private YxyHuodongHrbDeclarationRepository yxyHuodongHrbDeclarationRepository;

    @RequestMapping(value = "/api_yw/home/activity/hrb/declaration/sch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String userId = getProfile(request, response).getId();
        String roleId = getProfile(request, response).getRoleId();
        String schoolId = getProfile(request, response).getSchoolId();
        Integer loginStatus = 0;
        Integer status = 0;
        Integer noDecl = 0;
        RespModel model = new RespModel();
        if (null != roleId && "" != roleId) {
            // 教师
            if (roleId.equals("ROLE_TEACHER")) {
                status = 0;
            } else
            // 教育局管理员
            if (roleId.equals("ROLE_EDU_ADMIN")) {
                status = 1;
            } else if (roleId.equals("ROLE_SCH_ADMIN")) {
                status = 2;
            } else {
                status = 3;
            }
        } else {
            status = 3;
        }
        model.status = status;
        // 判断是否登录
        if (null == userId || !(userId.length() > 0)) {
            loginStatus = 0;
        } else {
            loginStatus = 1;
        }
        model.loginStatus = loginStatus;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd   hh:mm");
        List<Declaration> declarationList = new ArrayList<Declaration>();
        // 查询精品宣言
        Integer integer = 1;
        List<YxyHuodongHrbDeclaration> huodongHrbDeclarations = yxyHuodongHrbDeclarationRepository
                .findByIsBestOrderByCreatedDateDesc(integer);
        if (null != huodongHrbDeclarations && huodongHrbDeclarations.size() > 0) {
            noDecl = 1;
            for (YxyHuodongHrbDeclaration yxyHuodongHrbDeclaration : huodongHrbDeclarations) {
                Declaration declaration = new Declaration();
                declaration.createDate = dateFormat.format(yxyHuodongHrbDeclaration.getLastModifiedDate());
                declaration.declaration = yxyHuodongHrbDeclaration.getDeclaration();
                declaration.teacherId = yxyHuodongHrbDeclaration.getTeacherId();
                declaration.teacherNick = yxyHuodongHrbDeclaration.getTeacherNick();
                declaration.schoolNick = "( " + yxyHuodongHrbDeclaration.getSchoolNick() + " )";
                declaration.isBest = yxyHuodongHrbDeclaration.getIsBest();
                declaration.schoolId = yxyHuodongHrbDeclaration.getSchoolId();
                declarationList.add(declaration);
            }
        } else {
            noDecl = 0;
        }
        if (null != schoolId && "" != schoolId) {
            YxyHuodongHrbDeclaration teacherIdAndSchoolId = yxyHuodongHrbDeclarationRepository.findByTeacherId(userId);
            if (null != teacherIdAndSchoolId) {
                model.orderNum = teacherIdAndSchoolId.getOrderNum();
            } else {
                model.orderNum = 0;
            }

        } else {
            model.orderNum = 0;
        }

        model.declarations = declarationList;
        model.noDecl = noDecl;
        return model;
    }

    protected static class ReqModel extends YxyApiReqModel {

    }

    protected class Declaration {
        public String declaration;
        public String teacherNick;
        public String teacherId;
        public String createDate;
        public String schoolNick;
        public String schoolId;
        // 是否精品宣言
        public Integer isBest;
    }

    protected static class RespModel extends YxyApiRespModel {
        // 0 未登录 1 登录
        public Integer loginStatus;
        // 精品宣言
        public List<Declaration> declarations;
        // 登录身份 0教师 1教育局管理员 2 学校管理员角色
        public Integer status;
        // 我是第几个宣誓人
        public Integer orderNum;
        public Integer noDecl;

    }
}
